import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView,
    meta:{
      isRoot:true,
      keep:true,
    }
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/Login.vue'),
    meta:{
      isRoot:false
    }
  },
  {
    path: '/publish',
    name: 'publish',
    component: () => import('../views/Publish.vue'),
    meta:{
      isRoot:true,
      keep:true,
    }
  },
  {
    path: '/my',
    name: 'my',
    component: () => import('../views/My.vue'),
    meta:{
      isRoot:true,
      keep:true,
    }
  },
  {
    path: '/registered',
    name: 'registered',
    component: () => import('../views/Registered.vue'),
  },
  {
    path: '/article',
    name: 'article',
    component: () => import('../views/article/Article.vue'),
  },,
  {
    path: '/search',
    name: 'search',
    component: () => import('../views/search/Search.vue'),
    meta:{
    }
  },,
  {
    path: '/myift',
    name: 'myift',
    component: () => import('../views/myift/Myift.vue'),
    meta:{
      keep:true,
      auth:true,
    }
  },
  {
    path: '/about',
    name: 'about',
    component: () => import('../views/About.vue'),
    meta:{
    }
  },
  {
    path: '/contact',
    name: 'contact',
    component: () => import('../views/Contact.vue'),
    meta:{
    }
  },
  {
    path: '/forget',
    name: 'forget',
    component: () => import('../views/Forget.vue'),
    meta:{
      auth:true,
    }
  },
  {
    path: '/revise',
    name: 'revise',
    component: () => import('../views/Revise.vue'),
    meta:{
      auth:true,
    }
  },
  {
    path: '/searchList',
    name: 'searchList',
    component: () => import('../views/search/List.vue'),
    meta:{
    }
  },


  // {
  //   path: '/about',
  //   name: 'about',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  // },
]

const router = new VueRouter({
  routes
})

import store from "@/store"
import {Toast}from"vant"
// 路由守卫写在router后面   全局前置守卫  放行后才能访问页面
router.beforeEach((to,from,next)=>{
  //to  准备去哪个路由
  //from  从哪来的

  // 未登录不能去的页面
  let uid=store.state.uid
 
    // 敏感页面
  if(to.meta.auth){
    if(uid){
      // 登录
  
      next()//放行
    }else{
      // 未登录
      Toast('你还未登录，请登录后重试')
      setTimeout(v=>{
        router.replace('/login')
      },1000)
    }
  }else{
    // 不是敏感页面
  next()//放行
  }

})

export default router
